package main

import (
	"MyWebcrawler/imgcrawler/monitor"
	mylog "MyWebcrawler/log"
	"MyWebcrawler/scheduler"
	"flag"
	"fmt"
	"github.com/sirupsen/logrus"
	"net/http"
	"os"
	"time"
)

var (
	firstURL string
	domains  string
	depth    uint
	dirPath  string
)

func init() {
	flag.StringVar(&firstURL, "first", "https://clicli.com.cn/2912.html", "The first URL which you want to access.")
	flag.StringVar(&domains, "domains", "clicli.com.cn", "The primary domains which you accepted. "+"Please using comma-separated multiple domains.")
	flag.UintVar(&depth, "depth", 3, "The depth for crawling.")
	flag.StringVar(&dirPath, "dir", "./Picture/", "The path which you want to save the image files.")
}

func Usage() {
	fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
	fmt.Fprintf(os.Stderr, "\tfinder [flags] \n")
	fmt.Fprintf(os.Stderr, "Flags:\n")
}

func main() {
	flag.Usage = Usage
	flag.Parse()

	s := scheduler.NewScheduler()
	if s == nil {
		fmt.Println("nil scheduler")
		return
	}
	acceptedDomains := []string{}
	requestArgs := scheduler.RequestArgs{
		AcceptedDomains: acceptedDomains,
		MaxDepth:        uint32(depth),
	}

	dataArgs := scheduler.DataArgs{
		ReqBufferCap:         50,
		ReqMaxBufferNumber:   1000,
		RespBufferCap:        50,
		RespMaxBufferNumber:  10,
		ItemBufferCap:        50,
		ItemMaxBufferNumber:  100,
		ErrorBufferCap:       50,
		ErrorMaxBufferNumber: 1,
	}

	downloaders, err := scheduler.GetDownloaders(1)
	if err != nil {
		logrus.Fatalf("An error occurs when creating downloaders: %s", err)
	}
	analyzers, err := scheduler.GetAnalyzers(1)
	if err != nil {
		logrus.Fatalf("An error occurs when creating analyzers: %s", err)
	}
	pipelines, err := scheduler.GetPipelines(1, dirPath)
	if err != nil {
		logrus.Fatalf("An error occurs when creating pipelines: %s", err)
	}
	moduleArgs := scheduler.ModuleArgs{
		Downloaders: downloaders,
		Analyzers:   analyzers,
		Pipelines:   pipelines,
	}
	initErr := s.Init(requestArgs, dataArgs, moduleArgs)
	if initErr != nil {
		logrus.Fatal("schedule init err")
	}

	checkInterval := time.Second

	maxIdleCount := uint(5)
	// 开始监控。
	checkCountChan := monitor.Monitor(s, checkInterval, maxIdleCount, true, mylog.NewLog())

	firstReq, NewErr := http.NewRequest("GET", "https://clicli.com.cn/2912.html", nil)
	if NewErr != nil {
		logrus.Fatalf("new first request err")
	}
	StartErr := s.Start(firstReq)
	if StartErr != nil {
		logrus.Fatalf(fmt.Sprintf("An error occurs when starting scheduler: %s", err))
	}

	<-checkCountChan
}
